package com.baobao.dao;

import com.baobao.domain.Teacher;

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.skyway.spring.util.dao.AbstractJpaDao;

import org.springframework.dao.DataAccessException;

import org.springframework.stereotype.Repository;

import org.springframework.transaction.annotation.Transactional;

/**
 * DAO to manage Teacher entities.
 * 
 */
@Repository("TeacherDAO")
@Transactional
public class TeacherDAOImpl extends AbstractJpaDao<Teacher> implements
		TeacherDAO {

	/**
	 * Set of entity classes managed by this DAO.  Typically a DAO manages a single entity.
	 *
	 */
	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(Arrays.asList(new Class<?>[] { Teacher.class }));

	/**
	 * EntityManager injected by Spring for persistence unit BaoBao
	 *
	 */
	@PersistenceContext(unitName = "BaoBao")
	private EntityManager entityManager;

	/**
	 * Instantiates a new TeacherDAOImpl
	 *
	 */
	public TeacherDAOImpl() {
		super();
	}

	/**
	 * Get the entity manager that manages persistence unit 
	 *
	 */
	public EntityManager getEntityManager() {
		return entityManager;
	}

	/**
	 * Returns the set of entity classes managed by this DAO.
	 *
	 */
	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	/**
	 * JPQL Query - findTeacherByAccountContaining
	 *
	 */
	@Transactional
	public Set<Teacher> findTeacherByAccountContaining(String account) throws DataAccessException {

		return findTeacherByAccountContaining(account, -1, -1);
	}

	/**
	 * JPQL Query - findTeacherByAccountContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Teacher> findTeacherByAccountContaining(String account, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findTeacherByAccountContaining", startResult, maxRows, account);
		return new LinkedHashSet<Teacher>(query.getResultList());
	}

	/**
	 * JPQL Query - findTeacherByImgUrl
	 *
	 */
	@Transactional
	public Set<Teacher> findTeacherByImgUrl(String imgUrl) throws DataAccessException {

		return findTeacherByImgUrl(imgUrl, -1, -1);
	}

	/**
	 * JPQL Query - findTeacherByImgUrl
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Teacher> findTeacherByImgUrl(String imgUrl, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findTeacherByImgUrl", startResult, maxRows, imgUrl);
		return new LinkedHashSet<Teacher>(query.getResultList());
	}

	/**
	 * JPQL Query - findTeacherByCourseRoleContaining
	 *
	 */
	@Transactional
	public Set<Teacher> findTeacherByCourseRoleContaining(String courseRole) throws DataAccessException {

		return findTeacherByCourseRoleContaining(courseRole, -1, -1);
	}

	/**
	 * JPQL Query - findTeacherByCourseRoleContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Teacher> findTeacherByCourseRoleContaining(String courseRole, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findTeacherByCourseRoleContaining", startResult, maxRows, courseRole);
		return new LinkedHashSet<Teacher>(query.getResultList());
	}

	/**
	 * JPQL Query - findTeacherByAccount
	 *
	 */
	@Transactional
	public Set<Teacher> findTeacherByAccount(String account) throws DataAccessException {

		return findTeacherByAccount(account, -1, -1);
	}

	/**
	 * JPQL Query - findTeacherByAccount
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Teacher> findTeacherByAccount(String account, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findTeacherByAccount", startResult, maxRows, account);
		return new LinkedHashSet<Teacher>(query.getResultList());
	}

	/**
	 * JPQL Query - findTeacherByImgUrlContaining
	 *
	 */
	@Transactional
	public Set<Teacher> findTeacherByImgUrlContaining(String imgUrl) throws DataAccessException {

		return findTeacherByImgUrlContaining(imgUrl, -1, -1);
	}

	/**
	 * JPQL Query - findTeacherByImgUrlContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Teacher> findTeacherByImgUrlContaining(String imgUrl, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findTeacherByImgUrlContaining", startResult, maxRows, imgUrl);
		return new LinkedHashSet<Teacher>(query.getResultList());
	}

	/**
	 * JPQL Query - findTeacherByPhone
	 *
	 */
	@Transactional
	public Teacher findTeacherByPhone(String phone) throws DataAccessException {

		return findTeacherByPhone(phone, -1, -1);
	}

	/**
	 * JPQL Query - findTeacherByPhone
	 *
	 */

	@Transactional
	public Teacher findTeacherByPhone(String phone, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findTeacherByPhone", startResult, maxRows, phone);
			return (com.baobao.domain.Teacher) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	/**
	 * JPQL Query - findTeacherByCourseRole
	 *
	 */
	@Transactional
	public Set<Teacher> findTeacherByCourseRole(String courseRole) throws DataAccessException {

		return findTeacherByCourseRole(courseRole, -1, -1);
	}

	/**
	 * JPQL Query - findTeacherByCourseRole
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Teacher> findTeacherByCourseRole(String courseRole, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findTeacherByCourseRole", startResult, maxRows, courseRole);
		return new LinkedHashSet<Teacher>(query.getResultList());
	}

	/**
	 * JPQL Query - findAllTeachers
	 *
	 */
	@Transactional
	public Set<Teacher> findAllTeachers() throws DataAccessException {

		return findAllTeachers(-1, -1);
	}

	/**
	 * JPQL Query - findAllTeachers
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Teacher> findAllTeachers(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllTeachers", startResult, maxRows);
		return new LinkedHashSet<Teacher>(query.getResultList());
	}

	/**
	 * JPQL Query - findTeacherByBanjiId
	 *
	 */
	@Transactional
	public Set<Teacher> findTeacherByBanjiId(String banjiId) throws DataAccessException {

		return findTeacherByBanjiId(banjiId, -1, -1);
	}

	/**
	 * JPQL Query - findTeacherByBanjiId
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Teacher> findTeacherByBanjiId(String banjiId, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findTeacherByBanjiId", startResult, maxRows, banjiId);
		return new LinkedHashSet<Teacher>(query.getResultList());
	}

	/**
	 * JPQL Query - findTeacherByBanjiIdContaining
	 *
	 */
	@Transactional
	public Set<Teacher> findTeacherByBanjiIdContaining(String banjiId) throws DataAccessException {

		return findTeacherByBanjiIdContaining(banjiId, -1, -1);
	}

	/**
	 * JPQL Query - findTeacherByBanjiIdContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Teacher> findTeacherByBanjiIdContaining(String banjiId, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findTeacherByBanjiIdContaining", startResult, maxRows, banjiId);
		return new LinkedHashSet<Teacher>(query.getResultList());
	}

	/**
	 * JPQL Query - findTeacherByPhoneContaining
	 *
	 */
	@Transactional
	public Set<Teacher> findTeacherByPhoneContaining(String phone) throws DataAccessException {

		return findTeacherByPhoneContaining(phone, -1, -1);
	}

	/**
	 * JPQL Query - findTeacherByPhoneContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Teacher> findTeacherByPhoneContaining(String phone, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findTeacherByPhoneContaining", startResult, maxRows, phone);
		return new LinkedHashSet<Teacher>(query.getResultList());
	}

	/**
	 * JPQL Query - findTeacherByPassword
	 *
	 */
	@Transactional
	public Set<Teacher> findTeacherByPassword(String password) throws DataAccessException {

		return findTeacherByPassword(password, -1, -1);
	}

	/**
	 * JPQL Query - findTeacherByPassword
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Teacher> findTeacherByPassword(String password, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findTeacherByPassword", startResult, maxRows, password);
		return new LinkedHashSet<Teacher>(query.getResultList());
	}

	/**
	 * JPQL Query - findTeacherByPrimaryKey
	 *
	 */
	@Transactional
	public Teacher findTeacherByPrimaryKey(String phone) throws DataAccessException {

		return findTeacherByPrimaryKey(phone, -1, -1);
	}

	/**
	 * JPQL Query - findTeacherByPrimaryKey
	 *
	 */

	@Transactional
	public Teacher findTeacherByPrimaryKey(String phone, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findTeacherByPrimaryKey", startResult, maxRows, phone);
			return (com.baobao.domain.Teacher) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	/**
	 * JPQL Query - findTeacherByNameContaining
	 *
	 */
	@Transactional
	public Set<Teacher> findTeacherByNameContaining(String name) throws DataAccessException {

		return findTeacherByNameContaining(name, -1, -1);
	}

	/**
	 * JPQL Query - findTeacherByNameContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Teacher> findTeacherByNameContaining(String name, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findTeacherByNameContaining", startResult, maxRows, name);
		return new LinkedHashSet<Teacher>(query.getResultList());
	}

	/**
	 * JPQL Query - findTeacherByName
	 *
	 */
	@Transactional
	public Set<Teacher> findTeacherByName(String name) throws DataAccessException {

		return findTeacherByName(name, -1, -1);
	}

	/**
	 * JPQL Query - findTeacherByName
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Teacher> findTeacherByName(String name, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findTeacherByName", startResult, maxRows, name);
		return new LinkedHashSet<Teacher>(query.getResultList());
	}

	/**
	 * JPQL Query - findTeacherByPasswordContaining
	 *
	 */
	@Transactional
	public Set<Teacher> findTeacherByPasswordContaining(String password) throws DataAccessException {

		return findTeacherByPasswordContaining(password, -1, -1);
	}

	/**
	 * JPQL Query - findTeacherByPasswordContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Teacher> findTeacherByPasswordContaining(String password, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findTeacherByPasswordContaining", startResult, maxRows, password);
		return new LinkedHashSet<Teacher>(query.getResultList());
	}

	/**
	 * Used to determine whether or not to merge the entity or persist the entity when calling Store
	 * @see store
	 * 
	 *
	 */
	public boolean canBeMerged(Teacher entity) {
		return true;
	}
}
